<%@ include file="/en/jsp/static.jsp" %>
<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>
<%@ taglib uri="/linuxbox/Todo.tld" prefix="todo" %>

<jsp:include page="/en/jsp/header.jsp" flush="true"/>

<jsp:useBean id="home" class="linuxbox.todo.logic.BoxTodo"/>
<%
  String VIEW_TYPE = (String) session.getAttribute("todo.VIEW_TYPE");
  if (VIEW_TYPE==null)
  {
    VIEW_TYPE = "ALL";
    session.setAttribute("todo.VIEW_TYPE", VIEW_TYPE);
  }
  String SORTBY = (String) session.getAttribute("todo.SORTBY");
  if (SORTBY==null)
  {
    SORTBY = "priority";
    session.setAttribute("todo.SORTBY", SORTBY);
  }

  boolean sortAsc = true;
  String tmp = (String) session.getAttribute("todo.ASC");
  if (tmp!=null && tmp.length() > 0)
    sortAsc = new Boolean(tmp).booleanValue();
  else
    session.setAttribute("todo.ASC", "true");

  int curPage = 0;
  String PAGE = (String) request.getParameter("PAGE");
  if (PAGE != null)
    curPage = Integer.parseInt(PAGE);

  int entriesPerPage = sec.getConfigInt("todo", "General", "Entries Per Page");
  int totalPage = home.getTotalPages(curID, entriesPerPage, VIEW_TYPE);
  if (curPage >= totalPage)
    curPage = totalPage-1;

  java.util.Iterator it = home.getTodos(curID, VIEW_TYPE, SORTBY, sortAsc, entriesPerPage*curPage, entriesPerPage);
  Calendar today = Calendar.getInstance();
  int count = 0;
  String todoJSP = BoxStatic.jspDir+"todo/BoxTodo.jsp";
  String todoAction = BoxStatic.servletDir+"BoxTodoAction";
%>

<script Language="JavaScript">
  function confirmDel(entry)
  {
    return confirm("Are you sure to delete " + entry);
  }
  function onChangeViewType()
  {
    document.theForm.submit();
  }
</script>

<FORM action="<%=BoxStatic.servletDir%>BoxTodoAction" name="theForm" method="POST">

<table width="100%" height=26 align=middle border=0 cellpadding=0 cellspacing=0 bgcolor="#f4f4f4" style="font-size:16px;color:#d05000">
  <tr>
    <td>
      &nbsp;<img src="<%=BoxStatic.imageDir%>itemball.jpg" width=14 height=15 border=0 align="absmiddle">
      <b><%=boxName%> - Todo List</b>
    </td>
    <td align=right>
      <a href="<%=BoxStatic.servletDir%>BoxTodoSettingsAction?SubmitEdit=edit">
        Settings
      </a>
    </td>
  </tr>
</table>
<hr width=\"100%\" color=#E06000 align=left>

<table width="100%" border=0 cellpadding=0 cellspacing=0>
<tr><td>
  <table width="100%">
    <tr>
      <td>
        <input type="submit" name="SubmitEdit" value="Add a task">
      </td>
      <td>View
          <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
            <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_CURRENT) == 0) out.print(" selected ");%> value="<%=home.VT_CURRENT%>">Current</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_OVERDUE) == 0) out.print(" selected ");%> value="<%=home.VT_OVERDUE%>">Overdue</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_COMPLETED) == 0) out.print(" selected ");%> value="<%=home.VT_COMPLETED%>">Completed</option>
          </select>
      </td>
      <td align=right>
        <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=todoJSP%>" />
      </td>
    </tr>
  </table>
</td></tr>

<tr><td>
<%

  if ( it != null && it.hasNext())
  {
    String[] headers = {"Priority", "Subject", "Due Date", "Completed", "Category", "Start Date", "Delete"};
    String[] sortby = {"priority", "subject", "duedate", "complete", "category", "startdate", ""};
%>
  <TABLE cellSpacing=1 cellPadding=0 width="100%" border=0 bgColor=#dddddd>
    <TR bgColor=#7EC1F3>
      <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=sortAsc%>" action="<%=todoAction%>" />
    </TR>

    <common:Iteration name="entry" type="linuxbox.todo.logic.BoxTodoEntry" group="<%=it%>">
      <tr <% if ((count++ % 2) == 0) out.print("bgcolor=#f0f0f0"); %>>
        <TD><%=home.getPriorityString(entry.getPriority())%></TD>
        <TD><a href="<%=BoxStatic.servletDir%>BoxTodoAction?SubmitEdit=edit&ID=<%=entry.getID()%>"><%=entry.getSubject()%></a></TD>
        <TD>
          <%
              if (entry.getDueDate().before(new java.util.Date()))
                out.print("<font color=#ff0000>"+entry.getDueDateString()+"</font>");
              else
                out.print(entry.getDueDateString());
          %>
        </TD>
        <TD  align=center>
          <%
            if (entry.getComplete())
              out.print("<img SRC=\"" +BoxStatic.imageDir+ "check.gif\">");
          %>
        </TD>
        <TD><%=entry.getCategory()%></TD>
        <TD><%=entry.getStartDateString()%></TD>
        <TD  align=center ><a href="<%=BoxStatic.servletDir%>BoxTodoAction?SubmitDelete=del&ID=<%=entry.getID()%>"
              Onclick='return confirmDel("<%=entry.getSubject()%>")'
              ALT="Delete">Delete</a></TD>
      </TR>
    </common:Iteration>
  </TABLE>
</td></tr>

<tr><td>
  <table width="100%">
    <tr>
      <td>
        <input type="submit" name="SubmitEdit" value="Add a task">
      </td>
      <td>View
          <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
            <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_CURRENT) == 0) out.print(" selected ");%> value="<%=home.VT_CURRENT%>">Current</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_OVERDUE) == 0) out.print(" selected ");%> value="<%=home.VT_OVERDUE%>">Overdue</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_COMPLETED) == 0) out.print(" selected ");%> value="<%=home.VT_COMPLETED%>">Completed</option>
          </select>
      </td>
      <td align=right>
        <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=todoJSP%>" />
      </td>
    </tr>
  </table>
</td></tr>

<%
  }
  else
  {
%>
    <TABLE cellSpacing=1 cellPadding=1 width="100%" border=0 bgColor=#dddddd>
      <TR>
        <TD bgColor=#eeeeee>You have no tasks.</TD>
      </TR>

    </TABLE>
</td></tr>
<%
  }
%>
<tr><td align=center>
<br>
</td></tr>

<tr><td align=center>
  <table cellSpacing=1 cellPadding=1  border=0 bgColor=#eeeeee>
    <tr><td align=center>
    <b>Quick Add Task</b>
    </td></tr>
    <tr><td align=center>
      <table>
        <tr>
          <td>
            <b>Subject:</b> <input align=right type=text name=SUBJECT size=20 value="">
          </td>
          <td>
            <b>Due Date:</b>
            <select name="DUE_MONTH" size="1">
              <common:MonthList  selected="<%=today.get(Calendar.MONTH)%>" allownull="false" />
            </select>
            <select name="DUE_DAY" size="1">
              <common:DayList selected="<%=today.get(Calendar.DATE)%>" allownull="false" />
            </select>
            <select name="DUE_YEAR" size="1">
              <common:YearList selected="<%=today.get(Calendar.YEAR)%>" allownull="false" />
            </select>
          </td>
          <td>
            <input type="submit" name="SubmitSave" value="Add">
          </td>
        </tr>
      </table>
    </td></tr>
  </table>
</td></tr>

</table>
<hr width=\"100%\" color=#E06000 align=left>

</FORM>

<jsp:include page="/en/jsp/footer.jsp" flush="true"/>
